package com.fc.mall.portal.controller;

import com.fc.mall.base.object.Result;
import com.fc.mall.exception.ServiceException;
import com.fc.mall.portal.dto.AgentBasicInfo;
import com.fc.mall.portal.dto.DecodePhoneParam;
import com.fc.mall.portal.dto.LoginReqDto;
import com.fc.mall.portal.dto.WxUserInfo;
import com.fc.mall.portal.service.UmsMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * 会员登录注册管理Controller
 * Created by dobuy on 2018/8/3.
 */
@RestController
@RequestMapping("/sso")
@Api(tags = "UmsMemberController", description = "会员登录注册管理")
public class UmsMemberController {
    @Autowired
    private UmsMemberService memberService;

    @ApiOperation("注册")
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public Result register(@RequestParam String username,
                           @RequestParam String password,
                           @RequestParam String telephone,
                           @RequestParam String authCode) {
        return memberService.register(username, password, telephone, authCode);
    }

    @ApiOperation("获取验证码")
    @RequestMapping(value = "/getAuthCode", method = RequestMethod.GET)
    public Result getAuthCode(@RequestParam String telephone) {
        return memberService.generateAuthCode(telephone);
    }

    @ApiOperation("登陆")
    @PostMapping(value = "/login")
    public Result login(@RequestBody LoginReqDto loginReqDto) {
        if(loginReqDto.getJsCode() == null)
        {
            throw new ServiceException("jsCode不能为空！");
        }
        return memberService.wxLogin(loginReqDto);
    }

    @ApiOperation("修改密码")
    @PostMapping(value = "/updatePassword")
    public Result updatePassword(@RequestParam String telephone,
                                 @RequestParam String password,
                                 @RequestParam String authCode) {
        return memberService.updatePassword(telephone,password,authCode);
    }

    @ApiOperation("更新会员信息")
    @PostMapping(value = "/update")
    public Result update(@RequestBody WxUserInfo wxUserInfo)
    {
        memberService.updateMemberInfo(wxUserInfo);
        return Result.success();
    }

    @ApiOperation("获取会员详细信息")
    @GetMapping(value = "/member/info")
    public Result getMemberInfo()
    {
        return memberService.getMemberDetail();
    }


    @ApiOperation("生成小程序推荐二维码")
    @PostMapping(value = "/generate/qrCode")
    public Result generateQRCode()
    {
        String page = "pages/index/index";
        byte[] qrCode = memberService.generateQrCode(page, new HashMap<>());
        return Result.success(qrCode);
    }


    @ApiOperation("获取代理商基本信息")
    @GetMapping("/agent/info")
    public Result handleGetAgentBasicInfo()
    {
        AgentBasicInfo agentBasicInfo = memberService.getAgentBasicInfo();
        return  Result.success(agentBasicInfo);
    }

    @PostMapping("/decode/phone")
    public Result handleDecodeMemberPhone(@RequestBody DecodePhoneParam decodePhoneParam)
    {
        return Result.success(memberService.updateMemberWxAppPhone(decodePhoneParam));
    }
}
